import { defineStore } from "pinia";
import { getMenuList } from "@/api/user";
import { MenuListType } from "@/type/index";

export const useUserStore = defineStore("user", {
    state: () => ({
        userInfo: {
            menuList: [] as MenuListType[],
            tabList: [] as MenuListType[]
        },
    }),
    actions: {
        getUserMenuList() {
            getMenuList().then((res) => {
                this.userInfo.menuList = res.data.data;
            });
        },
        addTab(val: MenuListType) {
            const tab = this.userInfo.tabList.find((item) => item.name == val.name);
            if (!tab) {
                this.userInfo.tabList.push(val);
            }
        },
        updateTab(tabs: MenuListType[]) {
            this.userInfo.tabList = tabs
        },
        clearMenuList(){
            this.userInfo.menuList = []
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'user',
                storage: localStorage
            }
        ]
    }
});
